package com.wh.Util;

import java.util.ArrayList;
import java.util.Random;

public class CodeUtil {
    public static String getCode() {

        //创建一个集合 包含大小写52个英文字母和10个数字
        ArrayList<Character> list = new ArrayList<>();
        //验证码
        StringBuilder sb = new StringBuilder();

        //向集合添加52个英文字母大小写
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }

        //随机获取四个英文字母
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }

        //随机获取两个数字
        for (int i = 0; i < 2; i++) {
            sb.append(random.nextInt(10));
        }

        //将里面的内容打乱
        for (int i = 0; i < sb.length(); i++) {
            int index = random.nextInt(sb.length());

            char temp = sb.charAt(i);
            sb.setCharAt(i, sb.charAt(index));
            sb.setCharAt(index, temp);
        }


        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(getCode());
    }
}